package com.example.lenovo.a203_yhy_ordering_3.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.adapter.FoodListAdapter;
import com.example.lenovo.a203_yhy_ordering_3.beans.FoodListBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.IsCollectBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.UserCollectFoodBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.UserCollectShopBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodListListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.IsCollectListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.UserCollectFoodListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.UserCollectShopListener;
import com.example.lenovo.a203_yhy_ordering_3.model.FoodListModel;
import com.example.lenovo.a203_yhy_ordering_3.model.IsCollectModel;
import com.example.lenovo.a203_yhy_ordering_3.model.UserCollectFoodModel;
import com.example.lenovo.a203_yhy_ordering_3.model.UserCollectShopModel;

import java.util.List;

public class FoodListActivity extends AppCompatActivity {
    private List<FoodListBean> foodListBeans;
    private RecyclerView recyclerView;
    private FoodListAdapter foodListAdapter;
    private int shop_id;
    //收藏
    private ImageView imageView7;
    private int user_id;
    private boolean isCollect;
    //评论
    private ImageView imageView8;
    private TextView textView3;
    private String shopname;
    private SharedPreferences sharedPreferences;


    //接收数据
    FoodListListener foodListListener=new FoodListListener() {
        @Override
        public void onResponse(List<FoodListBean> foodListBean) {
            foodListBeans = foodListBean;
            foodListAdapter.setList(foodListBeans);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"获取列表失败"+msg,Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);

        //收藏
        imageView7=findViewById(R.id.imageView7);
        //返回
        imageView8=findViewById(R.id.imageView8);
        //店铺名称
        textView3=findViewById(R.id.textView3);

        shop_id=getIntent().getIntExtra("shop_id",0);
        shopname=getIntent().getStringExtra("shopname");
        textView3.setText(shopname);

        sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
        user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));

        FoodListModel foodListModel=new FoodListModel();
        foodListModel.getFoodListResult(shop_id,foodListListener);

        initRecyclerView();
        isCollect();
        initEvents();


    }

    private void initRecyclerView() {
        recyclerView=findViewById(R.id.recyclerview);
        foodListAdapter=new FoodListAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL));

        FoodListAdapter.OnItemClickListener onItemClickListener= new FoodListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                Intent intent=new Intent(getApplicationContext(),FoodInfoActivity.class);
                intent.putExtra("food_id",foodListBeans.get(position).getFood_id());
                intent.putExtra("foodname",foodListBeans.get(position).getFoodname());
                intent.putExtra("price",foodListBeans.get(position).getPrice());
                intent.putExtra("intro",foodListBeans.get(position).getIntro());
                intent.putExtra("pic",foodListBeans.get(position).getPic());
                startActivity(intent);

            }
        };

        foodListAdapter.setOnItemClickListener(onItemClickListener);
        recyclerView.setAdapter(foodListAdapter);
    }

    private void initEvents() {
        //收藏
        imageView7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userCollectShop();
            }
        });
        //返回
        imageView8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void isCollect(){
        IsCollectModel isCollectModel=new IsCollectModel();
        isCollectModel.getIsCollectResult(user_id,shop_id,0,isCollectListener);
    }

    private void userCollectShop(){
        UserCollectShopModel userCollectShopModel = new UserCollectShopModel();
        userCollectShopModel.getUserCollectShopResult(user_id,shop_id,userCollectShopListener);
    }

    private UserCollectShopListener userCollectShopListener  =new UserCollectShopListener() {
        @Override
        public void onResponse(UserCollectShopBean userCollectShopBean) {
            if(userCollectShopBean.getSuccess().equals("0")){
                Toast.makeText(FoodListActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
            }
            else{
                if(isCollect){
                    isCollect=false;
                    imageView7.setImageResource(R.drawable.collect);
                    Toast.makeText(FoodListActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                }
                else {
                    isCollect=true;
                    imageView7.setImageResource(R.drawable.collect2);
                    Toast.makeText(FoodListActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                }
            }
        }


        @Override
        public void onFail(String msg) {
            Toast.makeText(FoodListActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };

    private IsCollectListener isCollectListener=new IsCollectListener() {
        @Override
        public void onResponse(IsCollectBean isCollectBean) {
            if(isCollectBean.getCollected().equals("0")){
                isCollect=false;
                imageView7.setImageResource(R.drawable.collect);
            }
            else {
                isCollect=true;
                imageView7.setImageResource(R.drawable.collect2);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FoodListActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };


}
